package com.efrei.uchat.client.gui.control;

import java.util.ArrayList;
import java.util.List;

import javax.swing.AbstractListModel;

public class UChatListModel<T> extends AbstractListModel {
	
	private static final long serialVersionUID = -5624804469525207645L;
	
	private List<T> elements = new ArrayList<T>();
	
	@Override
	public int getSize() {
		return elements.size();
	}
	
	public int size() {
		return getSize();
	}

	@Override
	public Object getElementAt(int index) {
		return elements.get(index);
	}
	
	public T get(int index) {
		return elements.get(index);
	}
	
	public void addElement(T elt) {
		elements.add(elt);
		int size = elements.size();
		fireContentsChanged(this, size-1, size);
	}
	
	public T removeAt(int index) {
		T elt = elements.remove(index);
		fireIntervalRemoved(this, index, index);
		return elt;
	}
	
	public boolean remove(T elt) {
		boolean ret = elements.remove(elt);
		if(ret)
			fireIntervalRemoved(this, 0, elements.size()+1);
		return ret;
	}
	
	public void clear() {
		clearAll();
	}
	
	public void clearAll() {
		int size = elements.size();
		elements.clear();
		fireIntervalRemoved(this, 0, size);
	}
}
